package com.example.chenzhiran.myapplication7.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.Statement;

public class likePage {

    private Socket socket;
    private clientData indata;
    private serverData outdata = new serverData();
    ObjectOutputStream out;
    Statement statement;

    public likePage(Socket mysocket, clientData mydata, Statement stmt)
    {
        this.socket = mysocket;
        this.indata = mydata;
        this.statement = stmt;

        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                if(name.equals(indata.username))
                {
                    finduser = true;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if(finduser == false) outdata.datastatus = 1;
        else
        {
            String sql = "select * from likelist where username = \"" + indata.username + "\"";
            String[] paths = new String[10];
            int pos = 0;
            try {
                ResultSet rs = statement.executeQuery(sql);
                while(rs.next()){
                    String path = rs.getString("product");
                    paths[pos++] = path;
                    FileInputStream fin = new FileInputStream(new File(path));
                    byte[] pic = new byte[fin.available()];
                    fin.read(pic);
                    outdata.imgs.put(path, pic);
                    fin.close();
                    outdata.datastatus = 0;
                }
                rs.close();

                getInfo(paths, pos);
            }catch (Exception e)
            {
                outdata.datastatus = 1;
                e.printStackTrace();
            }
        }

        try
        {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(outdata);
            out.close();
            System.out.printf("点赞页数据已发送\n");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void getInfo(String[] paths, int pos)
    {
        for(int i=0; i<pos; i++) {
            String path = paths[i];
            char[] picpath = path.toCharArray();
            char gender = picpath[10];
            String table;
            if (gender == 'm') {
                table = "malepic";
            } else table = "femalepic";

            String mysql = "select * from " + table + " where loc = \"" + path + "\"";
            try {
                ResultSet result = statement.executeQuery(mysql);
                while (result.next()) {
                    String describe = result.getString("des");
                    String sprice = result.getString("pri");
                    outdata.productdescribes.put(path, describe);
                    outdata.productprices.put(path, Integer.parseInt(sprice));
                }
                result.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
